package com.kkm.service.Imp;

import com.kkm.dao.Imp.OrderDaoImp;
import com.kkm.dao.OrderDao;
import com.kkm.pojo.Order;
import com.kkm.pojo.PageBean;
import com.kkm.service.OrderService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class OrderServiceImp  implements OrderService {
     private  OrderDaoImp orderDao=new OrderDaoImp();


    @Override
    public PageBean<Order> selectAll(String currentPage, int rows) {
        int currPage=Integer.parseInt(currentPage);
        if(currPage<=0){
            currPage=1;
        }
        PageBean<Order> pageBean=new PageBean(); //实例化pagebean对象
        int count= orderDao.checkOrderCount();//查询记录数

        int totalPage=count%rows==0?count/rows :count/rows+1;
        if(currPage>=totalPage){
            currPage=totalPage;
            System.out.println(currPage+"OrderService");
        }

        int index=(currPage-1)*rows;
        List<Order> orders=orderDao.showOrder(index,rows);

        pageBean.setRows(rows);
        pageBean.setCurrentPage(currPage);
        pageBean.setTotalPage(totalPage);
        pageBean.setTotalCount(count);
        pageBean.setList(orders);

        return pageBean;
    }


    @Override
    public PageBean<Order> checkOrder(Map<String, String[]> condition, String currentPage, int rows) {
        int orderCount=orderDao.checkOrderCount();
        int num=0;
        Set<String> keySet=condition.keySet();
        for(String key:keySet) {
            String value = condition.get(key)[0];//获取值
            if (!"".equals(value)) {
                num++;
            }
        }
        if(num>2){
            orderCount=orderDao.checkOrderNum(condition);
        }
        System.out.println(orderCount);
        if(orderCount>=1){
            int currPage=Integer.parseInt(currentPage);
            PageBean<Order> pageBean=new PageBean(); //实例化pagebean对象

            int totalPage=orderCount%rows==0?orderCount/rows :orderCount/rows+1;
            int index=(currPage-1)*rows;
            List<Order> orders=orderDao.checkOrder(condition,index,rows);

            pageBean.setRows(rows);
            pageBean.setCurrentPage(currPage);
            pageBean.setTotalPage(totalPage);
            pageBean.setTotalCount(orderCount);
            pageBean.setList(orders);
            return pageBean;
        }
        return null;
    }

    @Override
    public void deleteOrder(int oid) {
        orderDao.deleteOrder(oid);
    }

    @Override
    public Order orderById(int oid) {
        return orderDao.orderById(oid);
    }


    @Override
    public void modifyOrder(String address,int oid) {

        orderDao.modifyOrder(address,oid);
    }
}
